Repository navigation
perf(vm): close the fibonacci gap with Luerl — register reclaim + lazy call frames - #360
Merged
Merged
Conversation
Every Lua call pushed a 4-key `%{source, line, name, namewhat}` map onto
`call_stack`, eagerly running `hint_name/1` and `hint_namewhat/1` to
decode the call-site name tag. Those fields are read only by error
traceback formatting and `debug.getinfo` — both cold paths — so the work
is wasted on every successful call.
Store a flat 3-tuple `{source, line, name_hint}` instead and decode the
name lazily at the cold readers via new `Executor.frame_{source,line,
name,namewhat}/1` accessors (which also still accept caller-supplied map
frames, as the public `ErrorFormatter` entry points and tests pass).
Profiling fib(26) on the dispatcher: `dispatcher_call_info/3` drops from
5.0% to 2.0% of CPU and the two `hint_*` functions leave the profile
entirely; per-frame allocation falls from 7 words to 4.
`Expr.Call` left `next_reg` at `base + 1 + arg_count`, but a single-result call's result lives in `base_reg` and the callee/argument/ vararg temp registers above it are dead. Nothing reclaimed them, so sibling subexpressions stacked fresh registers on top: `fib(n-1) + fib(n-2)` compiled the two calls into disjoint register ranges (1-4 and 5-8) and reported `max_registers = 10`. Reset `next_reg` to `base_reg + 1` after the call (recording the peak first so the spread still counts; the `instruction_size` backstop scans the emitted writes regardless). The second call now reuses the freed registers and `max_registers` for fib drops to 6. Since every register file is a tuple sized to `max_registers`, this shrinks `setelement`, `copy_regs`, `make_tuple`, and `init_callee_regs` proportionally. On fib(30) vs Luerl the time gap closes from 1.18x to 1.03x (parity within noise) and total allocations fall ~26%.
This was referenced Jun 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes the recursive-fibonacci performance gap with Luerl by attacking the two costs the profiler flagged. On
fib(30), the time gap shrinks from 1.18x slower → 1.03x (parity within Luerl's own ±3.3% run-to-run noise) and total allocations fall ~26%.Driven data-first with the profiling loop: baseline →
mix profile.tprof(time + memory) → one fix → re-baseline.What the profile showed
On
fib(26)through the dispatcher::erlang.setelement/3Dispatcher.dispatch/8Executor.dispatcher_call_info/3hint_name/1+hint_namewhat/1Changes
1. Defer call-frame name decoding (
perf(vm))Every call eagerly built a 4-key
%{source, line, name, namewhat}map and decoded the call-site name tag — but those fields are read only by error traceback formatting anddebug.getinfo, both cold. Now stores a flat 3-tuple{source, line, name_hint}and decodes lazily at the cold readers.dispatcher_call_infodrops 5.0%→2.0% CPU; thehint_*functions leave the profile; per-frame alloc 7→4 words.2. Reclaim register window after single-result calls (
perf(codegen))Expr.Callleftnext_regabove the dead argument registers, sofib(n-1) + fib(n-2)stacked the two calls into disjoint register ranges and reportedmax_registers = 10. Resettingnext_regtobase_reg + 1after the call lets siblings reuse the freed registers — fib drops tomax_registers = 6. Since each register file is a tuple sized tomax_registers, this shrinkssetelement/copy_regs/make_tuple/init_callee_regsproportionally. This is the dominant win.Results (full-mode
benchmarks/fibonacci.exs, vs Luerl)Memory remains 1.13x Luerl, dominated by
setelementon the now-smaller tuples — further reduction is structural (the register representation itself).Testing
mix test --include slow— 2413 passed, 0 failures (includes the official Lua 5.3 suite)mix test --only differential --only lua53— all pass